/**
 * 指针目标删除接口测试用例
 *
 * Created by snowingsea on 2020/03/07.
 */
const chai = require('chai');
const should = chai.should(); // eslint-disable-line no-unused-vars
chai.use(require('chai-http'));
const randomstring = require('randomstring');

const common = require('./../../../common');
const Administrator = require('./../../../../models/administrator');
const uri = require('./../../uri');
const benchmark = require('./../../../../benchmark/analyse-files/logic_bombs');

module.exports = describe('/DELETE functions/:id/malloc/:name.json', function () {
  const administrator = new Administrator();
  const newUserIds = [];
  let projectVersion = {};
  let func = {};
  function url(functionId, variableName) {
    return `/functions/${functionId}/malloc/${variableName}.json`;
  }

  before(function (done) {
    administrator.login()
      .then(() => administrator.createRandomTester())
      .then((tester) => {
        newUserIds.push(tester.userId);
        return tester.createRandomProject();
      })
      .then((project) => project.createRandomVersion())
      .then((res) => {
        projectVersion = res;
        return projectVersion.getAllFunctions(benchmark);
      })
      .then(([item]) => {
        func = item;
        done();
      })
      .catch(done);
  });
  after(function (done) {
    administrator.removeUsers(newUserIds)
      .then(() => {
        done();
      })
      .catch(done);
  });

  common.should.be.user(uri, url, 'delete');
  common.should.be.tester(uri, url, 'delete');

  // 正常删除
  it('it should DELETE a malloc variable', function (done) {
    const variableName = randomstring.generate({ length: 16, charset: 'alphabetic' });
    func.addMallocVariable(variableName, { typeName: 'int' })
      .then(() => projectVersion.project.creator.login())
      .then(() => {
        chai.request(uri)
          .delete(url(func.functionId, variableName))
          .set('Authorization', `Bearer ${projectVersion.project.creator.token.accessToken}`)
          .end((err, res) => {
            res.should.have.status(204);

            // 查询
            chai.request(uri)
              .get(url(func.functionId, variableName))
              .set('Authorization', `Bearer ${projectVersion.project.creator.token.accessToken}`)
              .end((err, res) => {
                res.should.have.status(404);
                common.should.be.errorMessage(res.body);
                done();
              });
          });
      })
      .catch(done);
  });

  // 不可删除不存在的函数的指针目标
  it('it should fail to delete malloc variable when the function is not exists', function (done) {
    const variableName = randomstring.generate({ length: 16, charset: 'alphabetic' });
    new Promise((resolve) => {
      chai.request(uri)
        .delete(url('000000000000000000000000', variableName))
        .set('Authorization', `Bearer ${projectVersion.project.creator.token.accessToken}`)
        .end((err, res) => {
          res.should.have.status(404);
          common.should.be.errorMessage(res.body);
          resolve();
        });
    })
      .then(done)
      .catch(done);
  });

  // 不可删除他人项目的函数指针目标
  it('it should fail to delete malloc variable when the project is not yours', function (done) {
    const variableName = randomstring.generate({ length: 16, charset: 'alphabetic' });
    let newUser;
    administrator.createRandomTester()
      .then((tester) => {
        newUserIds.push(tester.userId);
        newUser = tester;
        return tester.getToken();
      })
      .then(() => {
        chai.request(uri)
          .delete(url(func.functionId, variableName))
          .set('Authorization', `Bearer ${newUser.token.accessToken}`)
          .end((err, res) => {
            res.should.have.status(404);
            common.should.be.errorMessage(res.body);
            done();
          });
      })
      .catch(done);
  });

  // 删除不存在的指针目标
  it('it should delete malloc variable when the variable is not exists', function (done) {
    chai.request(uri)
      .delete(url(func.functionId, randomstring.generate(16)))
      .set('Authorization', `Bearer ${projectVersion.project.creator.token.accessToken}`)
      .end((err, res) => {
        res.should.have.status(404);
        common.should.be.errorMessage(res.body);
        done();
      });
  });
});
